﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using Intorise.Common;
using System.Threading;

namespace MuBot
{
    public partial class MainForm : Form
    {
        Player player = null;
        //Account account = new Account { Username = "qq31808801", Password = "qq112112", ServerID = "1362" };
        //Account account = new Account { Username = "intorise", Password = "zpf800823", ServerID = "3613" };
        Account account = new Account { Username = "ss52131988", Password = "qq112112", ServerID = "3334" };//aa3323212   qq112112

        public MainForm()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
            //ConsoleBLL.TextBox = this.richTextBox1;
            ConsoleBLL.Log = new Loger(AppDomain.CurrentDomain.BaseDirectory + "\\log.txt");

        }

        #region event handler
        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(x =>
            {
                player = new Player(account, null);
                //player.AutoFight = true;
                if (player.Login())
                {
                    player.Play();
                }
                else
                {
                    ConsoleBLL.WriteLineError("登录失败");
                }
            }));
            thread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedItem != null)
            {
                player.Transmit(Maps.GetMap(this.comboBox1.SelectedItem.ToString()));
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //player.EnterMap();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int x, y;
            if (int.TryParse(this.txtPointX.Text, out x) && int.TryParse(this.txtPointY.Text, out y))
            {
                Thread thread = new Thread(new ParameterizedThreadStart(z =>
                {
                    player.GoTo(x, y);
                }));
                thread.Start();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text = "";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var data = Tools.GetByteFromHexString(this.txtData.Text);
            //player.SendData(data);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int line;
            if (!int.TryParse(this.txtLine.Text, out line))
            {
                MessageBox.Show("输入线编号"); return;
            }
            player.Change(line);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int serverID;
            if (!int.TryParse(this.txtServerID.Text, out serverID))
            {
                MessageBox.Show("服务器编号错误，请输入整数"); return;
            }
            if (this.txtUsername.Text == "" || this.txtPassword.Text == "")
            {
                MessageBox.Show("请输入帐号和密码"); return;
            }
            account = new Account { Username = this.txtUsername.Text, Password = this.txtPassword.Text, ServerID = this.txtServerID.Text };
            button1_Click(null, null);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            player.AutoFight = !player.AutoFight;
            if (player.AutoFight)
            {
                GameHelper.RunThreadPool(player.StartAutoFight);
                this.button9.Text = "停止挂机";
            }
            else
            {
                this.button9.Text = "开始挂机";
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(x =>
            {
                int steep = 2;
                int.TryParse(this.txtSteep.Text, out steep);
                Button button = (Button)sender;
                Point location = player.Location;
                switch (button.Text)
                {
                    case "上":
                        location.Y -= steep;
                        break;
                    case "下":
                        location.Y += steep;
                        break;
                    case "左":
                        location.X -= steep;
                        break;
                    case "右":
                        location.X += steep;
                        break;
                    case "左上":
                        location.X -= steep;
                        location.Y -= steep;
                        break;
                    case "左下":
                        location.X -= steep;
                        location.Y += steep;
                        break;
                    case "右上":
                        location.X += steep;
                        location.Y -= steep;
                        break;
                    case "右下":
                        location.X += steep;
                        location.Y += steep;
                        break;
                }
                player.GoTo(location);
            }));
            thread.Start();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            //player.BuyMedicine(Thing.MedicinalType.HP1);
        }

        private void button19_Click(object sender, EventArgs e)
        {
            //player.TakeMedicine(Thing.MedicinalType.HP1);
        }

        private void button20_Click(object sender, EventArgs e)
        {
            //player.BuyMedicine(Thing.MedicinalType.MP1);
        }

        private void button21_Click(object sender, EventArgs e)
        {
            //player.TakeMedicine(Thing.MedicinalType.MP1);
        }

        private void button22_Click(object sender, EventArgs e)
        {
            player.Ride();
        }
        #endregion

        public void WriterLineText(string text)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(x =>
                {
                    this.richTextBox1.AppendText(text + "\r\n");
                }), text);
            }
            else
            {
                this.richTextBox1.AppendText(text + "\r\n");
            }
        }
    }
}
